package com.lanyan.system.controller.system;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.lanyan.system.domain.vo.SettingItemDto;
import com.lanyan.system.domain.vo.SettingVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lanyan.common.annotation.Log;
import com.lanyan.common.core.controller.BaseController;
import com.lanyan.common.core.domain.AjaxResult;
import com.lanyan.common.enums.BusinessType;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import com.lanyan.system.domain.Setting;
import com.lanyan.system.service.ISettingService;
import com.lanyan.common.utils.poi.ExcelUtil;
import com.lanyan.common.core.page.TableDataInfo;

/**
 * 设置Controller
 *
 * @author lanyan
 * @date 2024-11-26
 */
@RestController
@RequestMapping("/system/setting")
public class SettingController extends BaseController
{
    @Autowired
    private ISettingService settingService;

    /**
     * 查询设置列表
     */
    @PreAuthorize("@ss.hasPermi('system:setting:list')")
    @GetMapping("/list")
    @ApiImplicitParams({
          @ApiImplicitParam(name = "createBy", value = "创建者", dataType = "String", dataTypeClass = String.class),
          @ApiImplicitParam(name = "createTime", value = "创建时间", dataType = "Date", dataTypeClass = Date.class),
          @ApiImplicitParam(name = "updateBy", value = "更新者", dataType = "String", dataTypeClass = String.class),
          @ApiImplicitParam(name = "updateTime", value = "更新时间", dataType = "Date", dataTypeClass = Date.class),
          @ApiImplicitParam(name = "remark", value = "备注", dataType = "String", dataTypeClass = String.class),
          @ApiImplicitParam(name = "groupKey", value = "组属性", dataType = "String", dataTypeClass = String.class),
          @ApiImplicitParam(name = "groupName", value = "设置组", dataType = "String", dataTypeClass = String.class),
          @ApiImplicitParam(name = "valueKey", value = "名属性", dataType = "String", dataTypeClass = String.class),
          @ApiImplicitParam(name = "valueName", value = "设置名", dataType = "String", dataTypeClass = String.class),
          @ApiImplicitParam(name = "value", value = "设置值", dataType = "String", dataTypeClass = String.class),
          @ApiImplicitParam(name = "groupSort", value = "组排序", dataType = "Long", dataTypeClass = Long.class),
          @ApiImplicitParam(name = "valueSort", value = "值排序", dataType = "Long", dataTypeClass = Long.class),
          @ApiImplicitParam(name = "selectValue", value = "可选值", dataType = "String", dataTypeClass = String.class),
          @ApiImplicitParam(name = "pageNum", value = "第几页", dataType = "int", dataTypeClass = int.class),
          @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", dataTypeClass = int.class),

    })
    public TableDataInfo list(Setting setting)
    {
        startPage();
        List<Setting> list = settingService.selectSettingList(setting);
        return getDataTable(list);
    }

    /**
     * 导出设置列表
     */
    @PreAuthorize("@ss.hasPermi('system:setting:export')")
    @Log(title = "设置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Setting setting)
    {
        List<Setting> list = settingService.selectSettingList(setting);
        ExcelUtil<Setting> util = new ExcelUtil<Setting>(Setting.class);
        util.exportExcel(response, list, "设置数据");
    }

    /**
     * 获取设置详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:setting:query')")
    @GetMapping(value = "/{settingId}")
    public AjaxResult getInfo(@PathVariable("settingId") Long settingId)
    {
        return success(settingService.selectSettingBySettingId(settingId));
    }

    /**
     * 新增设置
     */
    @PreAuthorize("@ss.hasPermi('system:setting:add')")
    @Log(title = "设置", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiImplicitParams({
                    @ApiImplicitParam(name = "createBy", value = "创建者", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "createTime", value = "创建时间", dataType = "Date", dataTypeClass = Date.class),
                    @ApiImplicitParam(name = "updateBy", value = "更新者", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "updateTime", value = "更新时间", dataType = "Date", dataTypeClass = Date.class),
                    @ApiImplicitParam(name = "remark", value = "备注", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "groupKey", value = "组属性", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "groupName", value = "设置组", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "valueKey", value = "名属性", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "valueName", value = "设置名", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "value", value = "设置值", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "groupSort", value = "组排序", dataType = "Long", dataTypeClass = Long.class),
                    @ApiImplicitParam(name = "valueSort", value = "值排序", dataType = "Long", dataTypeClass = Long.class),
                    @ApiImplicitParam(name = "selectValue", value = "可选值", dataType = "String", dataTypeClass = String.class),
    })
    public AjaxResult add(@RequestBody Setting setting)
    {
        return toAjax(settingService.insertSetting(setting));
    }

    /**
     * 修改设置
     */
    @PreAuthorize("@ss.hasPermi('system:setting:edit')")
    @Log(title = "设置", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiImplicitParams({
                    @ApiImplicitParam(name = "createBy", value = "创建者", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "createTime", value = "创建时间", dataType = "Date", dataTypeClass = Date.class),
                    @ApiImplicitParam(name = "updateBy", value = "更新者", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "updateTime", value = "更新时间", dataType = "Date", dataTypeClass = Date.class),
                    @ApiImplicitParam(name = "remark", value = "备注", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "groupKey", value = "组属性", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "groupName", value = "设置组", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "valueKey", value = "名属性", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "valueName", value = "设置名", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "value", value = "设置值", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "groupSort", value = "组排序", dataType = "Long", dataTypeClass = Long.class),
                    @ApiImplicitParam(name = "valueSort", value = "值排序", dataType = "Long", dataTypeClass = Long.class),
                    @ApiImplicitParam(name = "selectValue", value = "可选值", dataType = "String", dataTypeClass = String.class),
    })
    public AjaxResult edit(@RequestBody Setting setting)
    {
        return toAjax(settingService.updateSetting(setting));
    }

    /**
     * 删除设置
     */
    @PreAuthorize("@ss.hasPermi('system:setting:remove')")
    @Log(title = "设置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{settingIds}")
    public AjaxResult remove(@PathVariable Long[] settingIds)
    {
        return toAjax(settingService.deleteSettingBySettingIds(settingIds));
    }

    @GetMapping("getGroup")
    public AjaxResult getGroup(){
        List<Setting> list = settingService.getGroup();
        return success(list);
    }

    @GetMapping("getAll")
    public AjaxResult getAll(){
        List<Setting> list = settingService.lambdaQuery()
                .eq(SettingVo::getDelFlag, "0")
                .orderByAsc(SettingVo::getGroupSort, SettingVo::getValueSort)
                .list();
        Map<String, List<Setting>> map = list.stream().collect(Collectors.groupingBy(SettingVo::getGroupKey));
        List<Map> res = new ArrayList<>();
        for (String key:map.keySet()){
            Map item = new HashMap();
            List<Setting> settingList = map.get(key);
            item.put("groupKey",settingList.get(0).getGroupKey());
            item.put("groupName",settingList.get(0).getGroupName());
            item.put("settingList",settingList);
            res.add(item);
        }
        return success(res);
    }

    @PostMapping("submitAll")
    public AjaxResult submitAll(@RequestBody List<SettingItemDto> list){
        for (SettingItemDto itemDto : list) {
            List<Setting> settingList = itemDto.getSettingList();
            for (Setting item : settingList) {
                settingService.updateById(item);
            }
        }
        return success();
    }
}
